/*
        Firesoft 
        
    Copyright (C) 2010, Arttu Viljakainen 
    Published under GNU General Public License V3 
    (see http://www.gnu.org/licenses/licenses.html)
    
    This file is part of Firesoft.

    Firesoft is free software: 
    you can redistribute it and/or modify it under the terms of the GNU General 
    Public License as published by the Free Software Foundation, 
    either version 3 of the License, or (at your option) any later version.

    Firesoft is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
    Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Tool for Path Finding Algorithm Testing. If not, see         
    <http://www.gnu.org/licenses/>.
 */
package softstarter.gui.subareas.settings.itemlist;

import java.awt.Color;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import softstarter.gui.subareas.settings.SettingsView;
import softstarter.gui.subareas.settings.SettingsViewListener;
import softstarter.launching.RunnableItem;

public class EditableRunnableItemList {

	private JPanel masterContainer;
	private JScrollPane masterScroller;
	private JPanel editableItemContainer;
	private JPanel dragAndDropCounsel;
	
	private List<EditableRunnableItem> editableRunnableItems;
	private SettingsViewListener settingsViewListener;
	private SettingsView settingsView;
	
	private EditableRunnableItemChangeListener itemChangeListener;
	
	public EditableRunnableItemList(SettingsView settingsView, SettingsViewListener settingsViewListener) {
		this.settingsViewListener = settingsViewListener;
		this.settingsView = settingsView;
		build();
	}
	
	private void build() {
		editableRunnableItems = new ArrayList<EditableRunnableItem>();
		
		masterContainer = new JPanel();
		masterContainer.setOpaque(false);
		masterContainer.setPreferredSize(new Dimension(410, 315));
		masterContainer.setBorder(BorderFactory.createCompoundBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4), BorderFactory.createLineBorder(Color.ORANGE)));
		
		editableItemContainer = new JPanel();
		editableItemContainer.setOpaque(false);
		editableItemContainer.setLayout(new BoxLayout(editableItemContainer, BoxLayout.Y_AXIS));
		
		dragAndDropCounsel = new JPanel();
		dragAndDropCounsel.setOpaque(false);
		dragAndDropCounsel.add(new JLabel("<html><h2>Drag & drop files"));
		
		masterScroller = new JScrollPane(editableItemContainer);
		masterScroller.setPreferredSize(new Dimension(400, 300));
		masterScroller.setOpaque(false);
		masterScroller.getViewport().setOpaque(false);
		masterScroller.setViewportBorder(BorderFactory.createEmptyBorder());
		masterScroller.setBorder(BorderFactory.createEmptyBorder());
		
		itemChangeListener = new EditableRunnableItemChangeListener(settingsView);
	}
	
	public JComponent getView() {
		
		return masterContainer;
	}
	
	public void updateRunnableItemList(List<RunnableItem> runnableItems) {
		
		masterContainer.removeAll();
		
		editableItemContainer.removeAll();
		editableRunnableItems = new ArrayList<EditableRunnableItem>();
		
		for(RunnableItem item : runnableItems) {
			EditableRunnableItem editableItem = new EditableRunnableItem(item);
			editableItem.addRemoveButtonListener(settingsView);
			editableItem.addChangeListener(itemChangeListener);
			
			editableRunnableItems.add(editableItem);
			editableItemContainer.add(editableItem.getView());
		}
		
		if(editableItemContainer.getComponentCount() == 0) {
			masterContainer.add(dragAndDropCounsel);
		} else {
			masterContainer.add(masterScroller);
		}
	}

	public List<EditableRunnableItem> getEditableRunnableItems() {
		return editableRunnableItems;
	}
	
	
}
